<?php


namespace app\admin\controller\content;

use app\admin\controller\Base;

use app\common\model\VideoToVip;
use app\common\model\VideoVipLevel as models;
use app\common\traites\PublicCrudTrait;
use think\exception\ValidateException;
use \app\validate\VideoVipLevel as validates;

class VideoVip extends Base
{
    protected $model;
    use PublicCrudTrait;

    /**
     * 视频vip套餐控制器
     */
    public function initialize()
    {
        $this->model = new models();
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * 视频分类
     */
    public function index()
    {
        //当前页
        $page = $this->request->post('page/d', 1);
        //每页显示数量
        $limit = $this->request->post('limit/d', 10);
        $type = $this->request->post('type/d', 0);
        //查询搜索条件

        $list = $this->model->order('type desc,id desc');
            if ($type > 0){
                $list=$list->where('type',$type);
            }
        $list = $list->paginate(['list_rows' => $limit, 'page' => $page])->each(function($item, $key){
                isset($item->typeName[$item->type-1]['name']) && $item->video_type_name=$item->typeName[$item->type-1]['name'];
            });
        return $this->success($list);
    }

    /**
     * 添加
     */
    public function add()
    {
        //过滤数据
        $postField = 'title,price_single,price_vip,validity_time,status,type';
        $post = $this->request->only(explode(',', $postField), 'post', null);

        //验证数据
        try {
            validate(validates::class)->scene('add')->check($post);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $this->failed($e->getError());
        }

        $user = $this->model->where('title', $post['title'])->find();
        if ($user) return $this->failed('该套餐已存在');

        $save = $this->model->save($post);
        if ($save) return $this->success([]);
        return $this->failed('新增失败');
    }


    /**
     * 修改
     * @return mixed
     */
    public function edit()
    {
        //过滤数据
        $postField = 'id,title,price_single,price_vip,validity_time,status,type';
        $post = $this->request->only(explode(',', $postField), 'post', null);

        //验证数据
        try {
            validate(validates::class)->scene('edit')->check($post);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $this->failed($e->getError());
        }

        //查询是否重复
        $find = $this->model->where('title', $post['title'])->where('id', '<>', $post['id'])->find();
        if ($find) return $this->failed('该套餐已存在');

        //执行修改数据
        $save = $this->model->update($post);
        if ($save) return $this->success([]);
        return $this->failed('修改失败');
    }

    /**
     * 视频分配套餐
     */
    public function video_auth()
    {
        //过滤数据
        $postField = 'id,types';
        $post = $this->request->only(explode(',', $postField), 'post', null);

        //验证数据
        try {
            validate(validates::class)->scene('type')->check($post);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $this->failed($e->getError());
        }
        if (!is_array($post['types'])) return $this->failed('types数据错误');
        //type 为数组，转为字符串
        sort($post['types']);
        $post['types'] = implode(',', $post['types']) . ',';

        $model = new VideoToVip();
        //查询是否存在该视频套餐
        $find = $model->where('video_id', $post['id'])->find();

        //存在就修改,不存在就新增
        if (empty($find)) {
            $save = $model->insert(['video_id' => $post['id'], 'types' => $post['types']]);
        } else {
            $find->types = $post['types'];
            $save = $find->save();
        }

        if ($save) return $this->success([]);
        return $this->failed('分配失败');
    }

    //视频套餐名称列表
    public function type_name_list()
    {
        return $this->success($this->model->typeName);
    }

        //一键上架所有视频套餐
    public function fast_set_meal()
    {
        //获取拥有的套餐
        $select = $this->model->where(['status'=>1,'type'=>1])->select();
        if (empty($select)) return $this->failed('分配失败，没有套餐');

        $types=implode(',',array_column($select->toArray(),'id')) . ',';
        $save = (new VideoToVip)->where('types',null)->update(['types'=>$types]);
        if ($save) return $this->success([]);
        return $this->failed('已经分配过了');
    }
    //一键下架所有视频套餐
    public function end_set_meal()
    {
        $save = (new VideoToVip)->whereNotNull('types')->update(['types'=>null]);
        if ($save) return $this->success([]);
        return $this->failed('已经下架了所有套餐');
    }


}